package aes.client;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

import org.jfree.data.category.DefaultCategoryDataset;

import common.*;


public class ReportTeacherInfoUI extends JPanel implements ActionListener, Observer {
    /**
	 * 
	 */
	private static final long serialVersionUID = 8515529701453724890L;
	private ClientUI ui;
	private Client c;
	
	private JButton btnBack;
	private JTable list;
	private JPanel panel;
	private JTextField txtDateTime;
	private JTextField txtSubject;
	private JTextField txtCourse;
	private JTextField txtExamID;
	private JTextField txtExecutionCode;
	private JTextField txtMedian;
	private JTextField txtFinishedOnTime; 
	private JTextField txtActualTime;
	private JTextField txtExecuterID;
	private JTextField txtMean;
	private JTextField txtDidntMakeIt;
	private BarChart plot;
	private int code;
	
	public ReportTeacherInfoUI(Client c,ClientUI ui){
		this.c = c;
		this.ui = ui;
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.getContentPane().add(this);
		
		// Handle messages from server.
		c.addObserver(this);

		setLayout(new GridBagLayout());

		panel= new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));

		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowHeights = new int[]{16, 33, 33, 33, 33, 190, 33};
		gbl_panel.rowWeights = new double[]{1/7,1/7,1/7,1/7,1/7,1/7,1/7};
		
		gbl_panel.columnWidths = new int[]{100, 100, 100, 100, 100, 100};
		gbl_panel.columnWeights = new double[]{1/6,1/6,1/6,1/6,1/6,1/6,};
		panel.setLayout(gbl_panel);
		//////////////////////////////////////////////////////////////////
		JLabel lblTitle = new JLabel("Exam Details and Analysis");
		lblTitle.setVerticalAlignment(SwingConstants.TOP);
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setBounds(7, 10, 70, 17);
		lblTitle.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 6;
		gbc_lblTitle.fill = GridBagConstraints.BOTH;
		gbc_lblTitle.insets = new Insets(5, 5, 5, 5);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);	
		//////////////////////////////////////////////////////////////////		
		JLabel lblDateTime = new JLabel("Date and Time: ");
		lblDateTime.setBounds(7, 10, 10, 17);
		lblDateTime.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblDateTime = new GridBagConstraints();
		gbc_lblDateTime.anchor = GridBagConstraints.WEST;
		gbc_lblDateTime.insets = new Insets(5, 5, 5, 5);
		gbc_lblDateTime.gridx = 2;
		gbc_lblDateTime.gridy = 1;
		panel.add(lblDateTime, gbc_lblDateTime);	
		
		txtDateTime = new JTextField();
		txtDateTime.setEditable(false);
		txtDateTime.setColumns(10);
		
		GridBagConstraints gbc_txtDateTime = new GridBagConstraints();
		gbc_txtDateTime.anchor = GridBagConstraints.WEST;
		gbc_txtDateTime.insets = new Insets(5, 5, 5, 5);
		gbc_txtDateTime.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDateTime.gridx = 3;
		gbc_txtDateTime.gridy = 1;
		panel.add(txtDateTime, gbc_txtDateTime);
		//////////////////////////////////////////////////////////////////
		JLabel lblSubject = new JLabel("Subject: ");
		lblSubject.setBounds(7, 10, 10, 17);
		lblSubject.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblSubject = new GridBagConstraints();
		gbc_lblSubject.anchor = GridBagConstraints.WEST;
		gbc_lblSubject.insets = new Insets(5, 5, 5, 5);
		gbc_lblSubject.gridx = 0;
		gbc_lblSubject.gridy = 2;
		panel.add(lblSubject, gbc_lblSubject);	
		
		txtSubject = new JTextField();
		txtSubject.setEditable(false);
		txtSubject.setColumns(10);
		
		GridBagConstraints gbc_txtSubject = new GridBagConstraints();
		gbc_txtSubject.anchor = GridBagConstraints.WEST;
		gbc_txtSubject.insets = new Insets(5, 5, 5, 5);
		gbc_txtSubject.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtSubject.gridx = 1;
		gbc_txtSubject.gridy = 2;
		panel.add(txtSubject, gbc_txtSubject);
		//////////////////////////////////////////////////////////////////
		JLabel lblCourse = new JLabel("Course: ");
		lblCourse.setBounds(7, 10, 10, 17);
		lblCourse.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblCourse = new GridBagConstraints();
		gbc_lblCourse.anchor = GridBagConstraints.WEST;
		gbc_lblCourse.insets = new Insets(5, 5, 5, 5);
		gbc_lblCourse.gridx = 2;
		gbc_lblCourse.gridy = 2;
		panel.add(lblCourse, gbc_lblCourse);	
		
		txtCourse = new JTextField();
		txtCourse.setEditable(false);
		txtCourse.setColumns(10);
		
		GridBagConstraints gbc_txtCourse = new GridBagConstraints();
		gbc_txtCourse.anchor = GridBagConstraints.WEST;
		gbc_txtCourse.insets = new Insets(5, 5, 5, 5);
		gbc_txtCourse.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCourse.gridx = 3;
		gbc_txtCourse.gridy = 2;
		panel.add(txtCourse, gbc_txtCourse);
		//////////////////////////////////////////////////////////////////
		JLabel lblExamID = new JLabel("Exam ID: ");
		lblExamID.setBounds(7, 10, 10, 17);
		lblExamID.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblExamID = new GridBagConstraints();
		gbc_lblExamID.anchor = GridBagConstraints.WEST;
		gbc_lblExamID.insets = new Insets(5, 5, 5, 5);
		gbc_lblExamID.gridx = 4;
		gbc_lblExamID.gridy = 2;
		panel.add(lblExamID, gbc_lblExamID);	
		
		txtExamID = new JTextField();
		txtExamID.setEditable(false);
		txtExamID.setColumns(10);
		
		GridBagConstraints gbc_txtExamID = new GridBagConstraints();
		gbc_txtExamID.anchor = GridBagConstraints.WEST;
		gbc_txtExamID.insets = new Insets(5, 5, 5, 5);
		gbc_txtExamID.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtExamID.gridx = 5;
		gbc_txtExamID.gridy = 2;
		panel.add(txtExamID, gbc_txtExamID);
		
		//////////////////////////////////////////////////////////////////
		JLabel lblExecutionCode = new JLabel("Execution Code: ");
		lblExecutionCode.setBounds(7, 10, 10, 17);
		lblExecutionCode.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblExecutionCode = new GridBagConstraints();
		gbc_lblExecutionCode.anchor = GridBagConstraints.WEST;
		gbc_lblExecutionCode.insets = new Insets(5, 5, 5, 5);
		gbc_lblExecutionCode.gridx = 0;
		gbc_lblExecutionCode.gridy = 1;
		panel.add(lblExecutionCode, gbc_lblExecutionCode);	
		
		txtExecutionCode = new JTextField();
		txtExecutionCode.setEditable(false);
		txtExecutionCode.setColumns(10);
		
		GridBagConstraints gbc_txtExecutionCode = new GridBagConstraints();
		gbc_txtExecutionCode.anchor = GridBagConstraints.WEST;
		gbc_txtExecutionCode.insets = new Insets(5, 5, 5, 5);
		gbc_txtExecutionCode.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtExecutionCode.gridx = 1;
		gbc_txtExecutionCode.gridy = 1;
		panel.add(txtExecutionCode, gbc_txtExecutionCode);	
		//////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////
		JLabel lblMean = new JLabel("Mean: ");
		lblMean.setBounds(7, 10, 10, 17);
		lblMean.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblMean = new GridBagConstraints();
		gbc_lblMean.anchor = GridBagConstraints.WEST;
		gbc_lblMean.insets = new Insets(5, 5, 5, 5);
		gbc_lblMean.gridx = 0;
		gbc_lblMean.gridy = 4;
		panel.add(lblMean, gbc_lblMean);	
		
		txtMean = new JTextField();
		txtMean.setEditable(false);
		txtMean.setColumns(10);
		
		GridBagConstraints gbc_txtMean = new GridBagConstraints();
		gbc_txtMean.anchor = GridBagConstraints.WEST;
		gbc_txtMean.insets = new Insets(5, 5, 5, 5);
		gbc_txtMean.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtMean.gridx = 1;
		gbc_txtMean.gridy = 4;
		panel.add(txtMean, gbc_txtMean);
		//////////////////////////////////////////////////////////////////
		JLabel lblAverage = new JLabel("Median: ");
		lblAverage.setBounds(7, 10, 10, 17);
		lblAverage.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblAverage = new GridBagConstraints();
		gbc_lblAverage.anchor = GridBagConstraints.WEST;
		gbc_lblAverage.insets = new Insets(5, 5, 5, 5);
		gbc_lblAverage.gridx = 2;
		gbc_lblAverage.gridy = 4;
		panel.add(lblAverage, gbc_lblAverage);	
		
		txtMedian = new JTextField();
		txtMedian.setEditable(false);
		txtMedian.setColumns(10);
		
		GridBagConstraints gbc_txtMedian = new GridBagConstraints();
		gbc_txtMedian.anchor = GridBagConstraints.WEST;
		gbc_txtMedian.insets = new Insets(5, 5, 5, 5);
		gbc_txtMedian.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtMedian.gridx = 3;
		gbc_txtMedian.gridy = 4;
		panel.add(txtMedian, gbc_txtMedian);
		//////////////////////////////////////////////////////////////////
		JLabel lblDidntMakeIt = new JLabel("Didnt Make It: ");
		lblDidntMakeIt.setBounds(7, 10, 10, 17);
		lblDidntMakeIt.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblDidntMakeIt = new GridBagConstraints();
		gbc_lblDidntMakeIt.anchor = GridBagConstraints.WEST;
		gbc_lblDidntMakeIt.insets = new Insets(5, 5, 5, 5);
		gbc_lblDidntMakeIt.gridx = 0;
		gbc_lblDidntMakeIt.gridy = 3;
	//	gbc_lblDidntMakeIt.gridwidth=2;
		panel.add(lblDidntMakeIt, gbc_lblDidntMakeIt);	
		
		txtDidntMakeIt = new JTextField();
		txtDidntMakeIt.setEditable(false);
		txtDidntMakeIt.setColumns(10);
		
		GridBagConstraints gbc_txtDidntMakeIt = new GridBagConstraints();
		gbc_txtDidntMakeIt.anchor = GridBagConstraints.WEST;
		gbc_txtDidntMakeIt.insets = new Insets(5, 5, 5, 5);
		gbc_txtDidntMakeIt.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDidntMakeIt.gridx = 1;
		gbc_txtDidntMakeIt.gridy = 3;
		panel.add(txtDidntMakeIt, gbc_txtDidntMakeIt);		
		//////////////////////////////////////////////////////////////////
		JLabel lblFinishedOnTime = new JLabel("Finished On Time: ");
		lblFinishedOnTime.setBounds(7, 10, 10, 17);
		lblFinishedOnTime.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblFinishedOnTime = new GridBagConstraints();
		gbc_lblFinishedOnTime.anchor = GridBagConstraints.WEST;
		gbc_lblFinishedOnTime.insets = new Insets(5, 5, 5, 5);
		gbc_lblFinishedOnTime.gridx = 2;
		gbc_lblFinishedOnTime.gridy = 3;
		//gbc_lblFinishedOnTime.gridwidth=2;
		panel.add(lblFinishedOnTime, gbc_lblFinishedOnTime);	
		
		txtFinishedOnTime = new JTextField();
		txtFinishedOnTime.setEditable(false);
		txtFinishedOnTime.setColumns(10);
		
		GridBagConstraints gbc_txtFinishedOnTime = new GridBagConstraints();
		gbc_txtFinishedOnTime.anchor = GridBagConstraints.WEST;
		gbc_txtFinishedOnTime.insets = new Insets(5, 5, 5, 5);
		gbc_txtFinishedOnTime.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtFinishedOnTime.gridx = 3;
		gbc_txtFinishedOnTime.gridy = 3;
		panel.add(txtFinishedOnTime, gbc_txtFinishedOnTime);		
		//////////////////////////////////////////////////////////////////
		JLabel lblActualTime = new JLabel("Actual Time: ");
		lblActualTime.setBounds(7, 10, 10, 17);
		lblActualTime.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblActualTime = new GridBagConstraints();
		gbc_lblActualTime.anchor = GridBagConstraints.WEST;
		gbc_lblActualTime.insets = new Insets(5, 5, 5, 5);
		gbc_lblActualTime.gridx = 4;
		gbc_lblActualTime.gridy = 3;
		//gbc_lblActualTime.gridwidth=2;
		panel.add(lblActualTime, gbc_lblActualTime);	
		
		txtActualTime = new JTextField();
		txtActualTime.setEditable(false);
		txtActualTime.setColumns(10);
		
		GridBagConstraints gbc_txtActualTime = new GridBagConstraints();
		gbc_txtActualTime.insets = new Insets(5, 5, 5, 5);
		gbc_txtActualTime.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtActualTime.gridx = 5;
		gbc_txtActualTime.gridy = 3;
		panel.add(txtActualTime, gbc_txtActualTime);		
		
		//////////////////////////////////////////////////////////////////
		JLabel lblExecuterID = new JLabel("Executer ID: ");
		lblExecuterID.setBounds(7, 10, 10, 17);
		lblExecuterID.setFont(new Font("Tahoma", Font.BOLD, 10));
		
		GridBagConstraints gbc_lblExecuterID = new GridBagConstraints();
		gbc_lblExecuterID.anchor = GridBagConstraints.WEST;
		gbc_lblExecuterID.insets = new Insets(5, 5, 5, 5);
		gbc_lblExecuterID.gridx = 4;
		gbc_lblExecuterID.gridy = 1;
		//gbc_lblExecuterID.gridwidth=2;
		panel.add(lblExecuterID, gbc_lblExecuterID);	
		
		txtExecuterID = new JTextField();
		txtExecuterID.setEditable(false);
		txtExecuterID.setColumns(10);
		
		GridBagConstraints gbc_txtExecuterID = new GridBagConstraints();
		gbc_txtExecuterID.anchor = GridBagConstraints.WEST;
		gbc_txtExecuterID.insets = new Insets(5, 5, 5, 5);
		gbc_txtExecuterID.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtExecuterID.gridx = 5;
		gbc_txtExecuterID.gridy = 1;
		panel.add(txtExecuterID, gbc_txtExecuterID);		
		
		//////////////////////////////////////////////////////////////////
			
		//////////////////////////////////////////////////////////////////	
		btnBack = new JButton("Back", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/back.png")));
		btnBack.addActionListener(this);
		btnBack.setBounds(0, 0, 20, 10);
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.insets = new Insets(5, 5, 5, 5);
		gbc_btnBack.gridwidth = 6;
		gbc_btnBack.anchor = GridBagConstraints.WEST;
		gbc_btnBack.gridx = 0;
		gbc_btnBack.gridy = 6;
		panel.add(btnBack, gbc_btnBack);
		btnBack.setBounds(353, 7, 57, 23);
		//////////////////////////////////////////////////////////////////		
		list = new JTable(){
			/**
			 * 
			 */
			private static final long serialVersionUID = -5848421012480878482L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false; //Disallow the editing of any cell
			}};
			list.setModel(new DefaultTableModel(
					new Object[][] {
					},
					new String[] {
							"Grades", "Num of Students(%)"
					}
					) {
				/**
				 * 
				 */
				private static final long serialVersionUID = -4567261985989300298L;
				@SuppressWarnings("rawtypes")
				Class[] columnTypes = new Class[] {
					String.class, String.class
				};
				@SuppressWarnings({ "unchecked", "rawtypes" })
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
			});
			list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			list.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			list.getColumnModel().getColumn(0).setPreferredWidth(60);
			list.getColumnModel().getColumn(1).setPreferredWidth(100);
			list.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
			JScrollPane scrollPane = new JScrollPane(list);
			list.setFillsViewportHeight(true);
			GridBagConstraints gbc_list = new GridBagConstraints();
			gbc_list.insets = new Insets(5, 5, 5, 5);
			gbc_list.fill = GridBagConstraints.BOTH;
			gbc_list.gridwidth = 2;
			gbc_list.gridx = 4;
			gbc_list.gridy = 5;
			panel.add(scrollPane, gbc_list);
			
			int[] ds={0,0,0,0,0,0,0,0,0};
			plot = new BarChart(ds,ui);
			GridBagConstraints gbc_plot = new GridBagConstraints();
			gbc_plot.gridwidth = 4;
			gbc_plot.gridheight = 1;
			gbc_plot.anchor = GridBagConstraints.WEST;
			gbc_plot.fill = GridBagConstraints.BOTH;
			gbc_plot.insets = new Insets(5, 5, 5, 5);
			gbc_plot.gridx = 0;
			gbc_plot.gridy = 5;
			panel.add(plot.getChartPanel(),gbc_plot);
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.GET_EXAM_EXECUTION_REPORT)){
				//gets Questions array from server answer (query answer from __ )
				String[] info = (String[]) ms.getParams()[1];
				if (info != null) {
					// Bind data.
					DefaultTableModel model =(DefaultTableModel) list.getModel();
					int count=0;
					for(int i=11;i<20;i++)
						count+=Integer.parseInt(info[i]);
					model.setRowCount(0);
					int[] barDataSet=new int[9];
					String[] grades = {"0-54.9","55-64","65-69","70-74","75-79","80-84","85-89","90-94","95-100"};
					for (int i=11,j=0;(i<20 && j<9);i++,j++) {
						barDataSet[j]=(int)(Float.parseFloat(info[i])/(float)count*100);
						String[] row = new String[2];
						row[0] = grades[j];
						row[1] = String.valueOf(barDataSet[j]);
						model.addRow(row);
					}
					list.setModel(model);
					
					DefaultCategoryDataset cd = (DefaultCategoryDataset)plot.getChartPanel().getChart().getCategoryPlot().getDataset();
					cd.setValue(barDataSet[0], cd.getRowKey(0), cd.getColumnKey(0));
					cd.setValue(barDataSet[1], cd.getRowKey(0), cd.getColumnKey(1));
					cd.setValue(barDataSet[2], cd.getRowKey(0), cd.getColumnKey(2));
					cd.setValue(barDataSet[3], cd.getRowKey(0), cd.getColumnKey(3));
					cd.setValue(barDataSet[4], cd.getRowKey(0), cd.getColumnKey(4));
					cd.setValue(barDataSet[5], cd.getRowKey(0), cd.getColumnKey(5));
					cd.setValue(barDataSet[6], cd.getRowKey(0), cd.getColumnKey(6));
					cd.setValue(barDataSet[7], cd.getRowKey(0), cd.getColumnKey(7));
					cd.setValue(barDataSet[8], cd.getRowKey(0), cd.getColumnKey(8));
					
					txtExecutionCode.setText(info[0]);
					txtDateTime.setText(info[1]);
					txtExecuterID.setText(info[2]);
					txtSubject.setText(info[3]);
					txtCourse.setText(info[4]);
					txtExamID.setText(info[5]);
					txtActualTime.setText(info[6]);
					txtDidntMakeIt.setText(info[7]);
					txtFinishedOnTime.setText(info[8]);
					txtMean.setText(info[9]);
					txtMedian.setText(info[10]);
				}
			}
		}
	}
	
	/**
	 * Buttons click event handler
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnBack)) {
			// Back.
			ui.SetContent(ClientUI.TEACHER_REPORT_SCREEN);
		}	
	}
	
	public void setExecutionCode(int codeSelected){
		code=codeSelected;
		CommandMessage ms = new CommandMessage(CommandMessage.GET_EXAM_EXECUTION_REPORT,new Object[]{code});
		try {
			c.sendToServer(ms);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}